package hl.demosample.ui.fragment.weekhot

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.kingja.loadsir.core.LoadService
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.*
import hl.demosample.app.util.ColorUtil
import hl.demosample.app.weight.recycleview.SpaceItemDecoration
import hl.demosample.databinding.FragmentWeekHotBinding
import hl.demosample.ui.adapter.WeekHotAdapter
import hl.demosample.viewmodel.request.RequestWeekHotViewModel
import hl.demosample.viewmodel.state.WeekHotViewModel
import kotlinx.android.synthetic.main.fragment_week_hot.*
import kotlinx.android.synthetic.main.include_smart_list.*
import kotlinx.android.synthetic.main.include_smart_rv.*
import kotlinx.android.synthetic.main.include_smart_rv.listSmartRefresh
import kotlinx.android.synthetic.main.include_smart_rv.recyclerView
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/*
* path: DemoSample 
* Author: hl
* Create: 2021-06-29 17:55
* Changes (from 2021-06-29)
* Desc:
*/
class WeekHotFragment : BaseFragment<WeekHotViewModel, FragmentWeekHotBinding>() {

    private val page = 20

    private val mGirl = "Girl"

    private lateinit var weekHotAdapter: WeekHotAdapter

    override fun layoutId() = R.layout.fragment_week_hot

    /**数据层ViewModel**/
    private val requestWeekHotViewModel: RequestWeekHotViewModel by viewModels()

    /**界面状态管理者**/
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {

        toolbar.initClose("本周最热Girl") {
            nav().navigateUp()
        }

        weekHotAdapter = WeekHotAdapter(arrayListOf())

        //状态页配置
        loadsir = loadServiceInit(listSmartRefresh) {
            //点击重试时触发的操作
            loadsir.showLoading()
            requestWeekHotViewModel.weekHot(ColorUtil.randomSister(), mGirl, page)
        }
        mRecyclerView.init(LinearLayoutManager(context),weekHotAdapter,false)
            .addItemDecoration(SpaceItemDecoration(0, ConvertUtils.dp2px(5f)))

        listSmartRefresh.refresh {
            requestWeekHotViewModel.weekHot(ColorUtil.randomSister(), mGirl, page)
            listSmartRefresh.finishRefresh()
        }.loadMore {
            listSmartRefresh.finishLoadMore()
        }

        weekHotAdapter.run {
            setOnItemClickListener { _, view, position ->
                nav().navigateAction(R.id.action_mainfragment_to_photoViewFragment, Bundle().apply {
                    putString("url",weekHotAdapter.data[position].url)
                })
            }
        }
    }

    override fun lazyLoadData() {
        loadsir.showLoading()
        requestWeekHotViewModel.weekHot(ColorUtil.randomSister(), mGirl, page)
    }

    override fun createObserver() {
        requestWeekHotViewModel.mData.observe(viewLifecycleOwner, Observer {
            if (it.isSuccess) {
                loadsir.showSuccess()
                weekHotAdapter.setNewInstance(it.listData)
            } else {
                loadsir.showError(it.errMessage)
            }
        })

        appViewModel.run {
            //监听全局的列表动画改编
            appAnimation.observeInFragment(this@WeekHotFragment) {
                weekHotAdapter.setAdapterAnimation(it)
            }
        }
    }
}